home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / ANTENNA / YAGIU112 / GET_CMDL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-28  |  10.9 KB  |  363 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include "yagi.h"
  6.  
  7. extern char     *optarg;
  8. extern int optind, opterr;
  9. extern int errno;
  10. extern struct performance_data max, weight;
  11. extern double original_percent; 
  12. extern double percent;
  13. extern double magnitude, phase;
  14. extern double Zo; /* Z0 is defined in yagi.h, Zo can be set in optimise */
  15. extern double max_gain, boom_factor, diameter, best_perf;
  16. extern int popsize;
  17. extern int iterations, fitness_method;
  18. extern double vswr;
  19. extern double boom_sd, length_sd;
  20. extern int K_times, K_times_max;
  21. int errno;
  22. double min_offset_from_peak=0.0;
  23. double angular_stepsize_2=0.0;
  24.  
  25. void get_command_line_options(int argc, char **argv, struct flags *flag)
  26. {
  27.     int c;
  28.     /* Since not all complilers come with the 'getopts' functions, I've
  29.     get a source of it and put it in the distribution of YagiUda. 
  30.     To avoid conflics on unix systems, I've rename it to getoptions */
  31.    while((c=getoptions(argc,argv,"A:c:kwW:hvdg:Or:P:m:C:b:x:f:s:S:G:R:X:F:l:o:e:Z:p:t:T:K:")) != -1)
  32.    switch       (c) 
  33.    {
  34.              case 'k': /* To keep the old start point with the GA */
  35.                     flag ->kflg=1;
  36.                     break;
  37.              case 'C':     /* Optimise by making element current the same */
  38.                     flag -> Cflg=atoi(optarg);
  39.                     break;
  40.              case 'A': /* Automatically maximise */
  41.                     flag->Aflg=atoi(optarg)+1000;
  42.                     if(flag->Aflg <-1)
  43.                     {
  44.                         fprintf(stderr,"Aflg x, where (-1<= x<= directors)\n");
  45.                         exit(1);
  46.                     }
  47.                     break;
  48.              case 'W':  /* weighted inprovement */
  49.                     flag->Wflg=atoi(optarg);
  50.                     if(flag->Wflg <1 || flag->Wflg>64)
  51.                     {
  52.                         fprintf(stderr,"-Wx, where x=1(gain),2() etc AND\n");
  53.                         exit(1);
  54.                     }
  55.                     flag->Wflg+=32768;
  56.                   break;
  57.              case 'a':  /* Angular step size to use when pattern searching */
  58.                     flag->aflg=1;
  59.                     if(!isdigit(*optarg) && *optarg !='.')
  60.                     {
  61.                         error_message("Non numeric data entered for option -a which requires numeric data.\n");
  62.                         exit(1);
  63.                     }
  64.                     angular_stepsize_2=atof(optarg);
  65.                     if(angular_stepsize_2 < 0.0)
  66.                     {
  67.                         error_message("The '-a' requires a posistive float, signifying the angular stepsize to use when finding sidelobes.\n");
  68.                         exit(1);
  69.                     }
  70.                   break;
  71.              case 'P':  /* Get average level of pattern down */
  72.                     flag->Pflg=1;
  73.                     if(!isdigit(*optarg) && *optarg !='.')
  74.                     {
  75.                         error_message("Non numeric data entered for option -P which requires numeric data.\n");
  76.                         exit(1);
  77.                     }
  78.                     weight.sidelobe=atof(optarg);
  79.                     if(weight.sidelobe < 0.0)
  80.                     {
  81.                         error_message("The '-P' requires a posistive float, signifying the weight on the sidelobe level (default=1.0).\n");
  82.                         exit(1);
  83.                     }
  84.                   break;
  85.              case 'c':  /* Get average level of pattern down */
  86.                     if(!isdigit(*optarg) && *optarg !='.')
  87.                     {
  88.                         error_message("Non numeric data entered for option -P which requires numeric data.\n");
  89.                         exit(1);
  90.                     }
  91.                     max.sidelobe=atof(optarg);
  92.                     if(max.sidelobe < 0.0)
  93.                     {
  94.                         error_message("The '-P' requires a posistive float, signifying the sidelobe level down to aim for in dB (20 is reasonable).\n");
  95.                         exit(1);
  96.                     }
  97.                     break;
  98.              case 'm':  /* Get minimum deviation from theta=90, to condsider a sidelobe. Higher the gain antenna, the smaller it should be set. */
  99.                     flag->mflg=1;
  100.                     if(!isdigit(*optarg) && *optarg !='.')
  101.                     {
  102.                         error_message("Non numeric data entered for option -m which requires numeric data.\n");
  103.                         exit(1);
  104.                     }
  105.                     min_offset_from_peak=atof(optarg);
  106.                     if(min_offset_from_peak < 0.0|| min_offset_from_peak > 90)
  107.                     {
  108.                         error_message("The '-m' requires a posistive float, signifying the minimum offset in degrees from theta =90, to start considering a sidelobe, rather than the main beam.\n");
  109.                         exit(1);
  110.                     }
  111.                   break;
  112.              case 'w':  /* wide band ant wanted - avg at low, design and upper f */
  113.                     flag->wflg=2; /* do at three frequencies,  2 extra ones */
  114.                   break;
  115.              case 'r':   /* acceptable_resistance_error option */
  116.                     flag->rflg=1;
  117.                     if(!isdigit(*optarg) && *optarg !='.')
  118.                     {
  119.                         error_message("Non numeric data entered for option -r which requires numeric data.\n");
  120.                         exit(1);
  121.                     }
  122.                     max.r=atof(optarg);
  123.                     if(max.r < 0.0)
  124.                     {
  125.                         error_message("The '-r' option setting an acceptable vswr must >=0.0\n");
  126.                         exit(1);
  127.                     }
  128.                   break;
  129.              case 'x':   /* acceptable_reactance option */
  130.                     flag->xflg=1;
  131.                     if(!isdigit(*optarg) && *optarg !='.')
  132.                     {
  133.                         error_message("Non numeric data entered for option -x which requires numeric data.\n");
  134.                         exit(1);
  135.                     }
  136.                     max.x=atof(optarg);
  137.                     if(max.x < 0.0)
  138.                     {
  139.                         error_message("The '-x' option setting an acceptable reactance must >=0.0\n");
  140.                         exit(1);
  141.                     }
  142.                   break;
  143.              case 's':   /* acceptable_vswr option */
  144.                     flag->sflg=1;
  145.                     if(!isdigit(*optarg) && *optarg !='.')
  146.                     {
  147.                         error_message("Non numeric data entered for option -s which requires numeric data.\n");
  148.                         exit(1);
  149.                     }
  150.                     max.swr=atof(optarg);
  151.                     if(max.swr < 1.0)
  152.                     {
  153.                         error_message("The '-s' option setting an acceptable vswr must >= 1.\n");
  154.                         exit(1);
  155.                     }
  156.                   break;
  157.              case 'f':   /* acceptable_fb_ratio option */
  158.                     flag->fflg=1;
  159.                     if(!isdigit(*optarg) && *optarg !='.')
  160.                     {
  161.                         error_message("Non numeric data entered for option -f which requires numeric data.\n");
  162.                         exit(1);
  163.                     }
  164.                     max.fb=atof(optarg);
  165.                     if(max.fb < 0.0)
  166.                     {
  167.                         error_message("The '-f' option setting an acceptable FB ratio must >=0.0\n");
  168.                         exit(1);
  169.                     }
  170.                   break;
  171.              case 'F':   /* fb_ratio weight */
  172.                     flag->Fflg=1;
  173.                     if(!isdigit(*optarg) && *optarg !='.')
  174.                     {
  175.                         error_message("Non numeric data entered for option -F which requires numeric data.\n");
  176.                         exit(1);
  177.                     }
  178.                     weight.fb=atof(optarg);
  179.                     if(weight.fb < 0.0)
  180.                     {
  181.                         error_message("The '-F' option setting the weight for FB ratio must >=0.0\n");
  182.                         exit(1);
  183.                     }
  184.                   break;
  185.              case 'G':   /* fb_ratio weight */
  186.                     flag->Gflg=1;
  187.                     if(!isdigit(*optarg) && *optarg !='.')
  188.                     {
  189.                         error_message("Non numeric data entered for option -G which requires numeric data.\n");
  190.                         exit(1);
  191.                     }
  192.                     weight.gain=atof(optarg);
  193.                     if(weight.gain < 0.0)
  194.                     {
  195.                         error_message("The '-G' option setting the weight for gain must >=0.0\n");
  196.                         exit(1);
  197.                     }
  198.                   break;
  199.              case 'S':   /* weight of swr*/
  200.                     flag->Sflg=1;
  201.                     if(!isdigit(*optarg) && *optarg !='.')
  202.                     {
  203.                         error_message("Non numeric data entered for option -S which requires numeric data.\n");
  204.                         exit(1);
  205.                     }
  206.                     weight.swr=atof(optarg);
  207.                     if(weight.swr < 0.0)
  208.                     {
  209.                         error_message("The '-S' option setting the weight for swr must >=0.0\n");
  210.                         exit(1);
  211.                     }
  212.                   break;
  213.              case 'l':   /* percentage change in ele positions */
  214.                     flag->lflg=1;
  215.                     if(!isdigit(*optarg) && *optarg !='.'&& *optarg!='-')
  216.                     {
  217.                         error_message("Non numeric data entered for option -l which requires numeric data.\n");
  218.                         exit(1);
  219.                     }
  220.                     original_percent=atof(optarg);
  221.                   break;
  222.              case 'o':   /* optimise for gain, fb etc etc */
  223.                     if(!isdigit(*optarg))
  224.                     {
  225.                         error_message("Non numeric data entered for option -o which requires numeric data.\n");
  226.                         exit(1);
  227.                     }
  228.                     flag->oflg=atoi(optarg);
  229.                     if(flag->oflg< 0 || flag->oflg > 128)
  230.                     {
  231.                         error_message("The '-o' option setting the parameter(s) to optimise for must be in the range 0 to 128.\n");
  232.                         exit(1);
  233.                     }
  234.                   break;
  235.              case 'O':
  236.                     flag->Oflg=1;
  237.                     break;
  238.              case 'K':   /* Keep to original data, until K bad goes */
  239.                     flag->Kflg=1;
  240.                     if(!isdigit(*optarg))
  241.                     {
  242.                         error_message("Non numeric data entered for option -K which requires an integer.\n");
  243.                         exit(1);
  244.                     }
  245.                     K_times_max=atoi(optarg);
  246.                     if(K_times_max < 1 )
  247.                     {
  248.                         error_message("The '-K' option setting the number of attemps to stay with hte original data after a good one found, to avoid local optimums, must be an integer > 1.\n");
  249.                         exit(1);
  250.                     }
  251.                   break;
  252.              case 'b':   /* how long can boom be extended */
  253.                     flag->bflg=1;
  254.                     if(!isdigit(*optarg) && *optarg !='.')
  255.                     {
  256.                         error_message("Non numeric data entered for option -b which requires numeric data.\n");
  257.                         exit(1);
  258.                     }
  259.                     boom_factor=atof(optarg);
  260.                     if(boom_factor < 0.0)
  261.                     {
  262.                         error_message("The '-b' option setting the maximum permissable change in the boom length (in %%) must be >=0.0\n");
  263.                         exit(1);
  264.                     }
  265.                   break;
  266.              case 'Z':   /* Characteristic impedance */
  267.                     flag->Zoflg=1;
  268.                     if(!isdigit(*optarg) && *optarg !='.')
  269.                     {
  270.                         error_message("Non numeric data entered for option -Z which requires numeric data.\n");
  271.                         exit(1);
  272.                     }
  273.                     Zo=atof(optarg);
  274.                     if(Zo <= 0.0)
  275.                     {
  276.                         error_message("The '-Z' option setting Zo must be > 0.0\n");
  277.                         exit(1);
  278.                     }
  279.                   break;
  280.              case 'e':   /* type of element moved (driven, parasitic or both) */
  281.                     if(!isdigit(*optarg) )
  282.                     {
  283.                         error_message("Non numeric data entered for option -e which requires numeric data.\n");
  284.                         exit(1);
  285.                     }
  286.                     flag->eflg=atoi(optarg);
  287.                     if(flag->eflg < 0 || flag->eflg > 1024)
  288.                     {
  289.                         error_message("The '-e' option setting the type of elements moved must be an integer between 1 and 127\n");
  290.                         exit(1);
  291.                     }
  292.                     if(flag->eflg==0)
  293.                         printf("Thats odd, you dont want to move any elements (-e0 option)\n");
  294.                   break;
  295.              case 'h':
  296.                   flag->hflg=1;
  297.                     break;
  298.              case 'v':
  299.                     printf("version = %lf\n", version());
  300.                     break;
  301.              case 'd':
  302.                     flag->dflg=1;
  303.                     break;
  304.             case 'g':
  305.                 flag->gflg=atoi(optarg);
  306.                 if(flag->gflg<1 || flag->gflg>64)
  307.                 {
  308.                     fprintf(stderr,"-gx, where x=1 to 64\n");
  309.                     exit(1);
  310.                 }
  311.                 break;
  312.             case 'p':
  313.                 flag->pflg=1;
  314.                 if(!isdigit(*optarg) && *optarg !='.')
  315.                     {
  316.                         error_message("Non numeric data entered for option -p which requires numeric data.\n");
  317.                         exit(1);
  318.                     }
  319.                 popsize=atoi(optarg);
  320.                 if(popsize < 2 || popsize > 10000000)
  321.                 {
  322.                         error_message("The '-p' option setting the population size of the genetric algorithm, mush be between 2 amd 1000000\n");
  323.                         exit(1);
  324.                 }
  325.                 break;
  326.             case 't':
  327.                 flag->tflg=1;
  328.                 if(!isdigit(*optarg) && *optarg !='.')
  329.                     {
  330.                         error_message("Non numeric data entered for option -t which requires numeric data.\n");
  331.                         exit(1);
  332.                     }
  333.                 length_sd=atof(optarg); /* SD on lengths, in mm */
  334.                 if(length_sd < 0 )
  335.                 {
  336.                         error_message("The '-t' option setting the standard deviation of the element lengths must be > 0\n");
  337.                         exit(1);
  338.                 }
  339.                 break;
  340.             case 'T':
  341.                 flag->Tflg=1;
  342.                 if(!isdigit(*optarg) && *optarg !='.')
  343.                     {
  344.                         error_message("Non numeric data entered for option -T which requires numeric data.\n");
  345.                         exit(1);
  346.                     }
  347.                 boom_sd=atof(optarg); /* SD on lengths, in mm */
  348.                 if(boom_sd < 0 )
  349.                 {
  350.                         error_message("The '-T' option setting the standard deviation of the boom postions must be > 0\n");
  351.                         exit(1);
  352.                 }
  353.                 break;
  354.              case '?':
  355.                flag->errflg++;
  356.                 break;
  357.      }
  358.      if(flag->Oflg)
  359.      {
  360.         flag->oflg-=32768;
  361.      }
  362. }
  363.